import Axios from 'axios'
//设置公共的url


// export const HOST = 'http://127.0.0.1:8111/';
export const HOST = 'http://146.56.197.217:8000';

Axios.defaults.baseURL = HOST;


export const getHeaders = () => {
  let userInfo = localStorage.getItem("userInfo");
  userInfo = JSON.parse(userInfo);
  if (userInfo) {
    return userInfo.access_token;
  }
  return '';
};

let getQueryStr = (queryData) => {
  let queryStr = '';
  for (let query in queryData) {
    let obj = queryData[query];
    if (obj instanceof Array) {
      obj.forEach(lis => {
        if (queryStr) {
          queryStr = queryStr + '&' + query + '=' + lis;
        } else {
          queryStr = '?' + queryStr + query + '=' + lis;
        }
      })
    } else {
      if (queryStr) {
        queryStr = queryStr + '&' + query + '=' + queryData[query];
      } else {
        queryStr = '?' + queryStr + query + '=' + queryData[query];
      }
    }
  }
  return queryStr
};

export const getModelsChoices = () => {
  let headers = {
    'headers': {
      'ACCOUNTTOKEN': getHeaders(),
      // 'Content-Type': 'application/x-www-form-urlencoded'
    }
  };
  return Axios.get('basics/getchoices/', headers)
};

const getApi = (URL, queryData) => {
  //page,size 分页必备参数
  let queryStr = getQueryStr(queryData);
  let headers = {
    'headers': {'ACCOUNTTOKEN': getHeaders()}
  };
  return Axios.get(URL + queryStr, headers)
};

const updateApi = (URL, queryData, params) => {
  let queryStr = getQueryStr(queryData);
  let headers = {
    'headers': {'ACCOUNTTOKEN': getHeaders()}
  };
  return Axios.put(URL + queryStr, params, headers)
};

const addApi = (URL, params) => {
  let headers = {
    'headers': {'ACCOUNTTOKEN': getHeaders()}
  };
  return Axios.post(URL, params, headers)
};

const deleteApi = (URL, queryData) => {
  let queryStr = getQueryStr(queryData);
  let headers = {
    'headers': {'ACCOUNTTOKEN': getHeaders()}
  };
  return Axios.delete(URL + queryStr, headers)
};

// 通过接口类型得到对应的阿贾克斯方法
export const getApiFunc = (apiStatus) => {
  let apiDict = {
    'get': getApi,
    'update': updateApi,
    'post': addApi,
    'delete': deleteApi
  };
  return apiDict[apiStatus]
};

// 接口类型
export const getApiStatus = 'get';
export const updateApiStatus = 'update';
export const addApiStatus = 'post';
export const deleteApiStatus = 'delete';

// 所有的接口
export const loginUrl = 'account/login'; // 登录接口，支持 [post]
export const registerUrl = 'account/register'; // 注册接口，支持 [post]
export const userExitUrl = 'account/exit'; // 注册接口，支持 [post]
export const accountURL = 'account/account';  // 用户列表操作接口，支持 [post,get,update,delete]

export const OftenMyToolURL = 'basics/OftenMyTool/';  // 工具列表
export const choicesURL = 'basics/getchoices/';  // 项目所有的CHOICES 支持 [get]
export const databaseURL = 'basics/database/';  // 数据库接口，支持 [post,get,update,delete]
export const environmentURL = 'basics/environment/';  // 环境列接口，支持 [post,get,update,delete]
export const sqlURL = 'basics/sql/';  // sql列接口，支持 [post,get,update,delete]
export const testSqlURL = 'basics/testsql/';  // 测试sql执行结果 支持get
export const projectURL = 'basics/project/';// 项目接口,支持 [post,get,update,delete]
export const moduleURL = 'basics/module/';// 模块URL，支持 [post,get,update,delete]
export const casesURL = 'basics/cases/';  // 用例URL，支持 [post,get,update,delete]
export const casesDetailURL = 'basics/casesDetail/';  // 用例步骤URL，支持 [post,get,update,delete]
export const testCasesURL = 'basics/testCases/';  // 执行测试用例
export const contentTypeURL = 'basics/contentType/';  // contentTypeURL，支持 [post,get,update,delete]
export const contentTypeObjectURL = 'basics/contentTypeObject/';  // contentTypeObjectURL，支持 get
export const RandomStringURL = 'basics/RandomString/';  // 获取随机英文字符串 post

export const attachmentURL = 'basics/attachment/';  // 附件URL，支持 [post,get,update,delete]
// export const downloadFileURL = 'basics/downloadFile/';  // 下载附件

// 接口测试
export const apiURL = 'api/api/';  // 接口列表，支持 [post,get,update,delete]
export const testApiURL = 'api/testApi/';  // 测试api
export const apiDetailURL = 'api/apiDetail/';  // 接口列表，支持 [post,get,update,delete]
export const testApiDetailURL = 'api/testApiDetail/';  // 测试apiDetail

// web测试
export const ModulePageURL = 'web/ModulePage/';  // 页面列表，支持 [post,get,update,delete]
export const LabelElementURL = 'web/LabelElement/';  // 标签列表，支持 [post,get,update,delete]
export const StepsURL = 'web/Steps/';  // 步骤列表，支持 [post,get,update,delete]
export const TestStepsURL = 'web/TestSteps/';  // 步骤测试
export const SeleniumFuncURL = 'web/SeleniumFunc/';  // 执行函数列表，支持 [post,get,update,delete]
export const StepsRealizeURL = 'web/StepsRealize/';  // 步骤执行列表，支持 [post,get,update,delete]
export const StepsRealizeAscensionURL = 'web/StepsRealizeAscension/';  // 步骤提升


export const IntervalURL = 'task/IntervalSchedule/';  // 间隔任务
export const CrontabScheduleURL = 'task/CrontabSchedule/';  // 计划任务
export const PeriodicTaskURL = 'task/PeriodicTask/';  // 任务列表
export const SolarScheduleURL = 'task/SolarSchedule/';  // 太阳任务
export const TaskResultURL = 'task/TaskResult/';  // 太阳任务

export const MyShellURL = 'task/MyShell/';  // 服务管理
export const ExeShellURL = 'task/ExeShell/';  // 执行脚本

export const AutomatedTaskURL = 'Automated/AutomatedTask/';  // 自动化任务

export const TestAutomatedURL = 'Automated/TestAutomated/';  // 自动化任务测试
export const AutomatedTaskRecordURL = 'Automated/AutomatedTaskRecord/';  // 自动化任务测试

export const TestReportURL = 'Automated/TestReport/';  // 测试报告

